package com.example.tiptravel.view.adapter.post;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tiptravel.R;
import com.example.tiptravel.commom.utils.ImageLoader;
import com.example.tiptravel.entity.post.PostBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 张稳 on 2016/11/9.
 */

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {

    private List<PostBean.DataEntity.ResultEntity> data = new ArrayList<>();
    private Context context;

    public PostAdapter(List<PostBean.DataEntity.ResultEntity> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_list_type, parent, false);
        PostViewHolder holder = new PostViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(PostViewHolder holder, int position) {
        String userPhoto = data.get(position).getUser().getPhoto();
        ImageLoader.getInstance().displayCricleImage(context, userPhoto, holder.ivUserPhoto);
        String resourse = data.get(position).getResource().get(0).getUrl();
        ImageLoader.getInstance().displayImage(resourse, context, holder.ivResourse, R.mipmap.bg_image_more, R.mipmap.bg_image_more);
        holder.tvUserName.setText(data.get(position).getUser().getUname());
        holder.tvTime.setText(data.get(position).getCtime());
        holder.tvFeedContent.setText(data.get(position).getFeedContent());
        holder.tvSharecount.setText("" + data.get(position).getShareCount());
        holder.tvCommentcount.setText("" + data.get(position).getCommentCount());
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    public class PostViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_post_user_photo)
        ImageView ivUserPhoto;
        @BindView(R.id.tv_post_user_uname)
        TextView tvUserName;
        @BindView(R.id.tv_post_ctime)
        TextView tvTime;
        @BindView(R.id.iv_post_resourse)
        ImageView ivResourse;
        @BindView(R.id.tv_post_feedcontent)
        TextView tvFeedContent;
        @BindView(R.id.tv_post_sharecount)
        TextView tvSharecount;
        @BindView(R.id.tv_post_commentcount)
        TextView tvCommentcount;


        public PostViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
